window.GSearch = function(map,uiDiv){
	this.map = map;
	this.inputKeyword = null;
	this.searchImg = null;
//	this.types = ['restaurant','food','cafe','bakery','meal delivery','meal takeaway','lodging'];
	this.types = ['school'];
	this.radius=1000;
	this.infowindow = null;
	this.markersArray = [];
	if(uiDiv){
		this.uiDiv = uiDiv;
		this.init();
	}
}

window.GSearch.prototype = {
	init:function(){
		this.initUI();
	},
	
	initUI:function(){
		var me = this;
		var root = $("#"+this.uiDiv);
		this.inputKeyword = $("<input id='searchKeyword' type='text'></input>");
		this.inputKeyword.appendTo(root);
		this.searchImg = $("<img id='searchimg' alt='点击进行搜索' src='image/08.png'></img>");
		this.searchImg.appendTo(root);
		this.searchImg.click(function(){
			me.search();
		});
	},
	
	search:function(keyword){
		var me = this;
		var map = this.map;
      	var pyrmont = map.getCenter();
      	var keyword = keyword||this.inputKeyword.val();
      	var request = {
          location: pyrmont,
          radius: this.radius,
          keyword :keyword,
          types: this.types
        };
        this.infowindow = new google.maps.InfoWindow();
        var service = new google.maps.places.PlacesService(map);
        service.search(request, function(results, status){
        	me.callback(results, status);
        });
	},
	
	callback:function(results, status) {
        if (status == google.maps.places.PlacesServiceStatus.OK) {
        	this.clearMarkers();
          for (var i = 0; i < results.length; i++) {
            this.createMarker(results[i]);
          }
          //生成示例数据，从resultes中取，然后构造index.html中的placemaker标签
          /**var markersKML = $("<div></div>");
          for (var i = 0; i < results.length; i++) {
          	var result = results[i];
          	var my = $("#my").clone().appendTo(markersKML);
            my.attr("id",new Date().getTime()+""+i);
            my.find("name").text(result.name);
            my.find("description").text(result.vicinity);
            my.find("coordinates").text(result.geometry.location.lng()+","+result.geometry.location.lat());
            my.find("Data[name='maintype'] value").text("酒店");
            my.find("Data[name='subtype'] value").text("商务酒店");
          }
          var r = markersKML.html();
          r1 = r.replace(/placemark/g,"Placemark").replace(/extendeddata/g,"ExtendedData").replace(/data/g,"Data").replace(/point/g,"Point");
        */
        }
      },

      createMarker:function(place) {
      	var me = this;
      	var map = this.map;
        var placeLoc = place.geometry.location;
        var marker = new google.maps.Marker({
          map: map,
          position: place.geometry.location
        });
		this.markersArray.push(marker);
        google.maps.event.addListener(marker, 'click', function() {
          me.infowindow.setContent(place.name);
          me.infowindow.open(map, this);
        });
      },
      
      clearMarkers:function(){
      	if (this.markersArray) {
		    for (i in this.markersArray) {
		      this.markersArray[i].setMap(null);
		    }
		  }
		 this.markersArray.length=0;
      },
      
      showOverlays:function() {
		  if (this.markersArray) {
		    for (i in this.markersArray) {
		      this.markersArray[i].setMap(this.map);
		    }
		  }
		}
}